<template>
	<view>
		<view class="wrap">
			<view class="order-header">
				<view class="u-tabs-box">
					<u-tabs-swiper activeColor="#10beca" ref="tabs"
						:list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750">
					</u-tabs-swiper>
				</view>
				<!-- <view class="u-p-30 bg-white">
					<u-search v-model="key" :show-action="false" shape="square" clearabled @change="searchKeyword"></u-search>
				</view> -->
				<list-filter-panel :filterType="filterType" @filter-change="filterChange"></list-filter-panel>
			</view>
			<view class="grid-box" v-for="(res, index) in orderList" :key="res.id">
				<navigator class="order" @click="details(res)">
					<view class="top u-border-bottom u-p-b-20">
						<view class="left">
							<view class="u-tips-color">{{res.number}}</view>
							<view><text class="u-font-24 u-tips-color">由<text class="bold-txt">{{res.staff.name}}</text>创建</text></view>
						</view>
						<text v-if="res.state == '已完结'" class="color-success">{{ res.state }}</text>
						<text v-else-if="res.state == '已取消'" class="color-danger">{{ res.state }}</text>
						<text v-else-if="res.state == '已出库'" class="color-warning">{{ res.state }}<!-- <text>(待回录)</text> --></text>
						<text v-else>{{ res.state }}</text>
					</view>
					<!-- 上门地址与联系人 -->
					<view class="pack u-m-b-10 u-m-t-20">
						<view class="left-con">
							<view class="text in">出</view>
							<view class="border"></view>
							<view class="text" :class="res.manufacturer?'out':''">入</view>
						</view>
						<view class="u-flex u-p-l-20" :style="{'flex-direction': 'column','align-items': 'flex-start'}">
							<view class="item">
								<view>
									<!-- <view class="name" v-for="(i,k) in res.storenames" :key="k">{{i}}</view> -->
									<view class="name">{{res.store.name}}</view>
									<view>
										<text class="text">出库站点</text>
									</view>
								</view>
							</view>
							<view class="item" :style="{'align-items':'flex-end'}" v-if="res.packinorder">
								<view>
									<view class="name">{{res.packinorder.packname}}</view>
									<view>
										<text class="text">类型：打包站</text>
									</view>
								</view>
							</view>
							<view class="item" :style="{'align-items':'flex-end'}" v-if="res.manufacturer">
								<view>
									<view class="name">{{res.manufacturer.name}}</view>
									<view>
										<text class="text">类型：厂商</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					
					<view class="middle u-border-top u-p-t-20" v-if="res.state == '已完结'">
						<view class="u-flex" style="justify-content: space-between;">
							<view class="">
								<text class="label u-tips-color">核实出库重量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.sellweight}}</text>
									<text class="unit">公斤</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">核实出库数量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.sellquantity}}</text>
									<text class="unit">件</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">出库销售金额</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.sellamount}}</text>
									<text class="unit">元</text>
								</view>
							</view>
						</view>
					</view>
					
					<!-- 运输至厂商或打包站 -->
					<view class="bottom u-border-top u-p-t-10">
						<view class="more">
							<view class="u-flex-1">
								<text class="u-font-24 u-tips-color" v-if="res.supercargo.id > 0">由<text class="bold-txt">{{res.supercargo.name}}</text>负责运输</text>
								<!-- <text class="u-font-24 u-tips-color" v-if="res.packinorder">由<text class="bold-txt">{{res.packinorder.brothername}}</text>负责人</text> -->
							</view>
							<!-- <text class="u-font-24 u-tips-color">{{$u.timeFrom(new Date(res.endtime), false)}}</text> -->
						</view>
						<view class="more">
							<view class="u-flex-1">
								<text class="u-font-24 u-tips-color" v-if="res.state == '已出库'">{{res.endtime}}建单</text>
								<text class="u-font-24 u-tips-color" v-if="res.state == '已完结'">{{res.endtime}}结单</text>
								<text class="u-font-24 u-tips-color" v-if="res.state == '已取消'">{{res.endtime}}取消订单</text>
							</view>
							<u-icon name="clock" class="u-tips-color"></u-icon>
							<text class="u-font-24 u-tips-color">{{$u.timeFrom(new Date(res.endtime), false)}}</text>
						</view>
					</view>
				</navigator>
			</view>
			<view class="u-p-30">
				<u-loadmore :status="loadStatus"></u-loadmore>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	
	export default {
		props:{
			sendStoreId:{
				type: [Number,String],
				default: 0
			},
			filterType:{
				type: Array,
				default:()=>['time']
			}
		},
		data() {
			return {
				list: [
					// {
					// 	value: '',
					// 	name: '全部'
					// },
					// {
					// 	value: 0,
					// 	name: '待出库'
					// },
					// {
					// 	value: 1,
					// 	name: '已装车'
					// },
					{
						value: 1,
						name: '已出库'
					},
					{
						value: 2,
						name: '已完结'
					},
					{
						value: 3,
						name: '已取消'
					}
				],
				current: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: 'loadmore',
				PageIndex: 1,
				PageSize: 20,
				noMore: false,
				key: '',
				orderList: [],
				// 时间类型
				timeType: 0, //筛选时间类别：0全部，1今日，2昨日，3本月，4上月，5本季度，6上季度，7本年，9自定义时间段
				startDate: '', //开始时间
				endDate: '', //结束时间
				// 其他筛选条件
				filterStoreId: [],
			}
		},
		computed: {
			// ...mapState({
			// 	storeId: state => state.storeId
			// })
		},
		onLoad(options) {
			console.log(options.current)
			if(options.current>0 || options.current == 0 ){
				this.current = options.current
			}
		},
		mounted() {
			this.orderList = []
			this.PageIndex = 1
			this.getOrderList(this.current);
		},
		onPullDownRefresh() {
			this.pullDownRefresh()
		},
		onReachBottom(e) {
			this.reachBottom()
		},
		methods: {
			pullDownRefresh(){
				this.orderList = []
				this.PageIndex = 1
				this.getOrderList(this.current).then(() => {
					uni.stopPullDownRefresh(); //停止当前页面下拉刷新
				})
			},
			filterChange(obj){
				console.log('filterChange',obj)
				// for(let key in obj){
				// 	this.$set(this,key,obj[key])
				// }
				this.timeType = obj.timeType
				this.startDate = obj.startDate
				this.endDate = obj.endDate
				this.filterStoreId = obj.filterStoreId
				this.key = obj.key
				this.getOrderList(true);
			},
			// 查看详情
			details(item){
				uni.navigateTo({
					url: '/pages/shop/outbound/views/views?id='+item.id
				})
			},
			reachBottom() {
				// 此tab为空数据
				this.PageIndex += 1
				if (!this.noMore) {
					this.getOrderList(false).then((row) => {
						if (row.length == 0) {
							this.noMore = true
						}
					})
				}
			},
			// 页面数据
			getOrderList(reload = true) {
				if(reload){
					this.PageIndex = 1
					this.orderList = []
				}
				this.loadStatus = 'loading'
				
				// /Store/OutOrder/List
				//  /Store/TransferOrder/List 
				let storeIds = []
				if(this.sendStoreId > 0){
					storeIds = [this.sendStoreId]
				}else{
					if(this.filterStoreId > 0){
						storeIds = [this.filterStoreId]
					}
				}
				return this.$u.post('/Store/OutOrder/List', {
					"timeType": this.timeType,
					"startDate": this.startDate,
					"endDate": this.endDate,
					"storeIds": storeIds,
					"PageIndex": this.PageIndex,
					"PageSize": this.PageSize,
					"key": this.key,
					"state": this.list[this.current].value
					// "state": 3
				}).then((res) => {
					if (res.rows.length > 0) {
						res.rows.forEach((item) => {
							// item.storenames = item.storenames.split(',').filter((item)=>item)
							this.orderList.push(item)
						})
						if (res.rows.length < this.PageSize) {
							this.loadStatus = "nomore"
						} else {
							this.loadStatus = "loadmore"
						}
					} else {
						this.loadStatus = "nomore"
					}
					return res.rows
				})
			},
			// tab栏切换
			change(index) {
				this.PageIndex = 1;
				this.current = index;
				this.orderList = [];
				this.noMore = false;
				this.getOrderList();
			},
			searchKeyword(val){
				this.key = val
				this.PageIndex = 1;
				this.orderList = [];
				this.getOrderList();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}
	
	.swiper-box {
		flex: 1;
	}
	
	.swiper-item {
		height: 100%;
	}
	
	.pack{
	    display: flex;
		padding: 0 0 0 10rpx;
	    // align-items: center;
	    .item{
			// width:40%;
			// border: 1px solid $u-border-color;
			display: flex;
			padding: 0 20rpx;
			// border-radius: 10rpx;
			min-height: 120rpx;
			margin-top: 0!important;
	        .name{
	            font-size: 36rpx;
				margin-bottom: 10rpx;
	        }
			.text{
			    color: $u-color-gray;
				font-size: 24rpx;
			}
	    }
		.left-con{
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 20rpx;
			padding-bottom: 52rpx;
			.text{
				font-size: 24rpx;
				width: 40rpx;
				height: 40rpx;
				line-height: 40rpx;
				text-align: center;
				color: #fff;
				border-radius: 40rpx;
				background-color: $u-color-blue;
				&.in{
					background-color: $u-color-primary;
				}
				&.out{
					background-color: $u-color-orange;
				}
			}
			.border{
				border-left: 1px solid $u-border-color;
				flex: 1;
				width: 1px;
				margin: 10rpx 0;
			}
		}
	}
	
	.order {
		// width: 710rpx;
		// background-color: #ffffff;
		// margin: 20rpx auto;
		// border-radius: 20rpx;
		// box-sizing: border-box;
		// padding: 20rpx;
		// font-size: 28rpx;
	
		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
	
			.left {
				// display: flex;
				align-items: center;
	
				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					display: flex;
					align-items: center;
				}
			}
	
			.right {
				color: $u-type-warning-dark;
			}
		}
	
		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;
	
			.left {
				margin-right: 20rpx;
	
				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
			}
	
			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
					font-weight: bold;
				}
	
				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}
			}
	
			.right {
				margin-left: 10rpx;
				text-align: right;
	
				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}
	
				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
	
		.total {
			text-align: right;
			font-size: 24rpx;
	
			.total-price {
				font-size: 32rpx;
				color: $u-color-orange;
			}
		}
	
		.middle {
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
			.label{
				
			}
			.unit{
				font-size: 24rpx;
				// color: $u-tips-color;
			}
		}
	
		.bottom {
			// display: flex;
			margin-top: 20rpx;
			// justify-content: space-between;
			// align-items: center;
			.more{
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}
			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>
